package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.YongJinAdapter;
import com.lc.afbx.conn.GetApiCommissionExtract;
import com.lc.afbx.util.DateTimePickDialogUtil;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.TimeUtil;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CommissionCanWithdrawActivity extends BaseActivity implements View.OnClickListener {

    private TextView commission_can_total_tv;
    private TextView commission_can_endtime_tv;
    private TextView commission_can_starttime_tv;
    private LinearLayout commission_can_mingxi_title_ll;
    private String initDateTime;
    private String initEndTime;

//    private Button commission_can_bt;
//    private RelativeLayout commission_can_starttime_rl;
//    private RelativeLayout commission_can_endtime_rl;

    private ListView commission_can_lv;
    private List<GetApiCommissionExtract.Data> dataList = new ArrayList<>();
    private YongJinAdapter adapter;

    private GetApiCommissionExtract getApiCommissionExtract = new GetApiCommissionExtract(new AsyCallBack<GetApiCommissionExtract.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiCommissionExtract.Info info) throws Exception {

            commission_can_mingxi_title_ll.setVisibility(View.VISIBLE);

            dataList.addAll(info.dataList);
            adapter.notifyDataSetChanged();

            //数据请求成功就让显示  不成功就不显示
            commission_can_total_tv.setText(String.format("%.2f", info.commission));
            findViewById(R.id.totalCommision_llayout).setVisibility(View.VISIBLE);
            findViewById(R.id.commission_can_tixian_rl).setVisibility(View.VISIBLE);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, TimeUtil.times(getApiCommissionExtract.starttime) + "至" + TimeUtil.times(getApiCommissionExtract.endtime) + "无可提现佣金");
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_can);


        Calendar calendar = Calendar.getInstance();
        //dialog显示当前时间
        initDateTime = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) - 3) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        initEndTime = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";

        commission_can_endtime_tv = ((TextView) findViewById(R.id.commission_can_endtime_tv));
        commission_can_starttime_tv = ((TextView) findViewById(R.id.commission_can_starttime_tv));

//        //布局  添加点击监听
//        commission_can_starttime_rl = ((RelativeLayout) findViewById(R.id.commission_can_starttime_rl));
//        commission_can_endtime_rl = ((RelativeLayout) findViewById(R.id.commission_can_endtime_rl));
//        commission_can_bt = ((Button) findViewById(R.id.commission_can_bt));

        commission_can_lv = ((ListView) findViewById(R.id.commission_can_lv));

        commission_can_mingxi_title_ll = ((LinearLayout) findViewById(R.id.commission_can_mingxi_title_ll));

        commission_can_total_tv = ((TextView) findViewById(R.id.commission_can_total_tv));

        commission_can_lv.setFocusable(false);
        commission_can_lv.setAdapter(adapter = new YongJinAdapter(context, dataList));

        setAppCallBack(new MyNotifyCallback());

    }

//    private void setListener() {
//        commission_can_starttime_rl.setOnClickListener(this);
//        commission_can_endtime_rl.setOnClickListener(this);
//
//    }


    @Override
    public void onClick(View view) {

        DateTimePickDialogUtil dialogUtil = null;
        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.commission_can_tixian_rl:

                if (Float.valueOf(commission_can_total_tv.getText().toString()) < 100) {
                    MyToast.showToast("可提现总金额小于100", context);
                    return;
                }

                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < dataList.size(); i++) {

                    if (i==0) {

                        sb.append(dataList.get(i).id);


                    } else {

                        sb.append("," + dataList.get(i).id);

                    }

                }


                Intent intent = new Intent(context, CommissionWithdrawActivity.class);

                intent.putExtra(PrefrenceConfig.INTENT_TAG, commission_can_total_tv.getText().toString());

                intent.putExtra("ids", sb.toString());

                context.startActivity(intent);

                return;

            case R.id.commission_can_starttime_rl:

                dialogUtil = new DateTimePickDialogUtil(this, initDateTime);
                dialogUtil.dateTimePicKDialog(commission_can_starttime_tv);

                return;

            case R.id.commission_can_endtime_rl:
                dialogUtil = new DateTimePickDialogUtil(this, initEndTime);
                dialogUtil.dateTimePicKDialog(commission_can_endtime_tv);
                return;

            case R.id.commission_can_bt:

//                if (startTime.getText().toString().isEmpty() || endTime.getText().toString().isEmpty()) {
//                    UtilToast.show(context, "请选择时间");
//                    return;
//                }
//                String start = TimeUtil.dataOne(startTime.getText().toString());
//                String end = TimeUtil.dataOne(endTime.getText().toString());
//
//                if (Long.valueOf(end) - Long.valueOf(start) >= 0) {
//
//                    titleItem.setVisibility(View.VISIBLE);
//
//                    getApiCommissionExtract.starttime = start;
//                    getApiCommissionExtract.endtime = end;
//                    getApiCommissionExtract.userid = BaseApplication.BasePreferences.readUserId();
//
//                    getApiCommissionExtract.execute(this);
//                    return;
//                } else {
//                    UtilToast.show(context, "起始时间 大于 结束时间");
//                    return;
//                }

                new MyNotifyCallback().Refrush();
                return;

        }

    }

    class MyNotifyCallback implements AppCallBack {

        public void Refrush() {

            if (commission_can_starttime_tv.getText().toString().isEmpty() || commission_can_endtime_tv.getText().toString().isEmpty()) {
                UtilToast.show(context, "请选择时间");
                return;
            }
            String start = TimeUtil.dataOne(commission_can_starttime_tv.getText().toString());
            String end = TimeUtil.dataOne(commission_can_endtime_tv.getText().toString());

            if (Long.valueOf(end) - Long.valueOf(start) >= 0) {

                dataList.clear();

                getApiCommissionExtract.starttime = start;
                getApiCommissionExtract.endtime = end;
                getApiCommissionExtract.userid = BaseApplication.BasePreferences.readUserId();

                getApiCommissionExtract.execute(context);
                return;
            } else {
                UtilToast.show(context, "起始时间 大于 结束时间");
                return;
            }

        }


    }

}
